<template>
  <a-modal
    title="新增账单流水"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="房间名"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入房间名" v-decorator="['roomName', {rules: [{required: true, message: '请输入房间名！'}]}]" />
        </a-form-item>
        <a-form-item
          label="姓名"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入姓名" v-decorator="['name', {rules: [{required: true, message: '请输入姓名！'}]}]" />
        </a-form-item>
        <a-form-item
          label="来源 （字典 bill_water_source）"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select style="width: 100%" placeholder="请选择来源 （字典 bill_water_source）" v-decorator="['source', {rules: [{ required: true, message: '请选择来源 （字典 bill_water_source）！' }]}]">
            <a-select-option v-for="(item,index) in sourceData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="金额"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
        </a-form-item>
        <a-form-item
          label="实收日期"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择实收日期" v-decorator="['receiveDate',{rules: [{ required: true, message: '请选择实收日期！' }]}]" @change="onChangereceiveDate"/>
        </a-form-item>
        <a-form-item
          label="操作日期"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择操作日期" v-decorator="['operatingDate',{rules: [{ required: true, message: '请选择操作日期！' }]}]" @change="onChangeoperatingDate"/>
        </a-form-item>
        <a-form-item
          label="交易方式编码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select style="width: 100%" placeholder="请选择交易方式编码" v-decorator="['transaction', {rules: [{ required: true, message: '请选择交易方式编码！' }]}]">
            <a-select-option v-for="(item,index) in transactionData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="交易方式"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入交易方式" v-decorator="['transactionName', {rules: [{required: true, message: '请输入交易方式！'}]}]" />
        </a-form-item>
        <a-form-item
          label="经办人id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入经办人id" v-decorator="['managerId', {rules: [{required: true, message: '请输入经办人id！'}]}]" />
        </a-form-item>
        <a-form-item
          label="经办人"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入经办人" v-decorator="['manager', {rules: [{required: true, message: '请输入经办人！'}]}]" />
        </a-form-item>
        <a-form-item
          label="经办时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择经办时间" v-decorator="['managertTime',{rules: [{ required: true, message: '请选择经办时间！' }]}]" @change="onChangemanagertTime"/>
        </a-form-item>
        <a-form-item
          label="审核标志"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select style="width: 100%" placeholder="请选择审核标志" v-decorator="['auditFlag', {rules: [{ required: true, message: '请选择审核标志！' }]}]">
            <a-select-option v-for="(item,index) in auditFlagData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="审核人ID"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入审核人ID" v-decorator="['auditId', {rules: [{required: true, message: '请输入审核人ID！'}]}]" />
        </a-form-item>
        <a-form-item
          label="审核人"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入审核人" v-decorator="['auditName', {rules: [{required: true, message: '请输入审核人！'}]}]" />
        </a-form-item>
        <a-form-item
          label="审核时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择审核时间" v-decorator="['auditTime',{rules: [{ required: true, message: '请选择审核时间！' }]}]" @change="onChangeauditTime"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { billWaterAdd } from '@/api/modular/main/billwater/billWaterManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        sourceData: [],
        receiveDateDateString: '',
        operatingDateDateString: '',
        transactionData: [],
        managertTimeDateString: '',
        auditFlagData: [],
        auditTimeDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      add (record) {
        this.visible = true
        const sourceOption = this.$options
        this.sourceData = sourceOption.filters['dictData']('bill_water_source')
        const transactionOption = this.$options
        this.transactionData = transactionOption.filters['dictData']('common_status')
        const auditFlagOption = this.$options
        this.auditFlagData = auditFlagOption.filters['dictData']('common_status')
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.receiveDate = this.receiveDateDateString
            values.operatingDate = this.operatingDateDateString
            values.managertTime = this.managertTimeDateString
            values.auditTime = this.auditTimeDateString
            billWaterAdd(values).then((res) => {
              if (res.code === 200) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('新增失败')// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangereceiveDate(date, dateString) {
        this.receiveDateDateString = dateString
      },
      onChangeoperatingDate(date, dateString) {
        this.operatingDateDateString = dateString
      },
      onChangemanagertTime(date, dateString) {
        this.managertTimeDateString = dateString
      },
      onChangeauditTime(date, dateString) {
        this.auditTimeDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
